import torch
import sys
sys.path.append("..")
from basic_knowledge import d2lzh_pytorch as d2l


# 画图函数
def xyplot(x_vals, y_vals, name):
    d2l.set_figsize(figsize=(5, 2.5))
    d2l.plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy())
    d2l.plt.xlabel('x')
    d2l.plt.ylabel(name + '(x)')
    d2l.plt.show();
# 求函数导数
def get_grad(x, y, name):
    xyplot(x, y, 'relu')
    y.sum().backward()
    xyplot(x, x.grad, 'grad of tanh')


x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)


get_grad(x, x.relu(), 'relu')
x.grad.zero_()
get_grad(x, x.sigmoid(), 'sigmoid')
x.grad.zero_()
get_grad(x, x.tanh(), 'tanh')